/**
 * @ClassName: DicDataStatusDao
 * @author linsen
 * @date 2016-11-15
 */
package com.fzrskj.cloud.free.dic.dao;

import com.fzrskj.cloud.free.core.utils.CloudDbDrvUtils;
import com.fzrskj.cloud.free.dic.bean.DicDataStatus;
import ms.core.consts.SysErr;
import ms.core.db.IDbDrv;
import ms.core.object.CObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class DicDataStatusDao extends CObject{
	//private declarations
	private IDbDrv db=null;

	//constructor function to initialize database access object
	public DicDataStatusDao(){
		this.db = CloudDbDrvUtils.getDbUtilsDrv();
	}

	//to free resource
	@Override
	public void dispose(){
		this.db.closeConnection();
	}

	//to add new record to database by bean
	public int addDicDatastatus(DicDataStatus obj){
		String sql = "insert into dic_datastatus(hostType,value,status,statusName,statusType)"+
					 "values(?,?,?,?,?,?);";
		Object[] prm={obj.getHostType(),obj.getValue(),obj.getStatus(),obj.getStatusName(),obj.getStatusType()};
		int ret = db.executeSql(sql,prm);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to add new record to database by map
	public int addDicDatastatus(Map<String, Object> values){
		int ret = db.insertRecord("dic_datastatus",values);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to delete table record by id
	public int delDicDatastatus(int id){
		String sql="delete from dic_datastatus where id=?";
		int ret = db.executeSql(sql,new Object[]{id});
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to delete table record by cdt
	public int delDicDatastatus(Map<String, Object> cdt){
		int ret = db.deleteRecord("dic_datastatus",cdt);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to modify table record by values and condition
	public int modDicDatastatus(Map<String, Object> values, Map<String, Object> cdt){
		int ret = db.modifyRecord("dic_datastatus", values, cdt);
		String code = db.getErrCode();
		this.setErrCode(code);
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}

		return ret;
	}

	//to query dic_datastatus item for detail
	public DicDataStatus queryDDSByStatus(String hostType,String status,String statusType){
		String sql="select * from dic_datastatus where hostType=? and status=? and statusType=?";
		DicDataStatus ret = db.queryForBean(sql,DicDataStatus.class,new Object[]{hostType,status,statusType});
		String code=db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}
	
	public DicDataStatus queryDDSByValue(String hostType,String value,String statusType){
		String sql="select * from dic_datastatus where hostType=? and value=? and statusType=?";
		DicDataStatus ret = db.queryForBean(sql,DicDataStatus.class,new Object[]{hostType,value,statusType});
		String code=db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to query DicDatastatus record count
	public int queryDicDatastatus(Map<String, Object> cdt){
		//TODO please verify count field name
		int ret = db.queryRecordCount("dic_datastatus", cdt);
		this.setErrCode(db.getErrCode());
		return ret;
	}

	//to query DicDatastatus for record list with condition,if page or pagesize less than 0, means without page
	public List<DicDataStatus> queryDicDatastatusList(Map<String, Object> cdt, int page, int pagesize){
		List<DicDataStatus> ret = db.queryForBeans("dic_datastatus", "*", "id desc", DicDataStatus.class, cdt, page, pagesize);
		String code=db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}
	
	//查询状态列表
	public List<DicDataStatus> queryDicDataStatusList(Map<String, Object> cdt){
		List<Object> list = new ArrayList<Object>();
		StringBuilder sb=new StringBuilder("select * from dic_datastatus where 1=1 ");
		
		for(Map.Entry<String, Object> c: cdt.entrySet()){
			sb.append(" and "+c.getKey()+"=?");
			list.add(c.getValue());
		}
		sb.append(" order by id ");
		
		Object[] params = new Object[list.size()];
		for (int i = 0; i < list.size(); i++) {
			params[i] = list.get(i);
		}
		List<DicDataStatus> ret = db.queryForBeans(sb.toString(), DicDataStatus.class, params );
		this.setErrCode(db.getErrCode());
		return ret;
	}
}
